layui.use(['ax','admin','form','ztree','table'], function () {
    var $ = layui.$;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var $ZTree = layui.ztree;
    var table = layui.table;
    form.render("select")


    /**
     * 系统管理--部门管理
     */
    var Deal = {
        tableId: "dataTable",
        condition: {
            parentId: ""
        }
    };

    /**
     * 初始化表格的列
     */
    Deal.initColumn = function () {
        return [[
            // {type: 'checkbox'},
            {field: 'dataId', hide: true, sort: true, title: 'id'},
            {field: 'dataName', title: '资料名称',minWidth:'350'},
            {field: 'dataType', title: '资料要求'},
            {field: 'baseScore', title: '基础分'},
            {field: 'dataScore', title: '评判分'},
            {field: 'dataMonth', sort: true, title: '时间要求',templet: function (d) {
                    return d.dataMonth + "月底"
                }},
            {field: 'hasUpload', title: '是否完成'},
            {field:"status",title:"状态",hide:true},
            {field: 'file_name', title: '文件名称',templet:"#viewFileTpl"},
            {align: 'center', toolbar: '#tableBar', title: '操作', fixed:'right',width:'130'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Deal.search = function () {
        var queryData = {};
        queryData['condition'] = $("#dataName").val();
        queryData['year'] = $("#year").val();
        queryData['parentId'] = Deal.condition.parentId;
        table.reload(Deal.tableId, {where: queryData});
    };

    $("#btnSearch").click(function () {
        Deal.search();
    })

    /**
     * 选择树节点
     */
    Deal.onClickJob = function (e, treeId, treeNode) {
        Deal.condition.parentId = treeNode.id;
        Deal.search();
    }

    Deal.uploadFile = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '上传资料',
            content: Feng.ctxPath + '/deal/toUpload?dataId='+data.dataId,
            end: function () {
                admin.getTempData('formOk') && table.reload(Deal.tableId);
            }
        });
    }

    Deal.openFile = function (data) {
        var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
        var isIE = userAgent.indexOf("compatible") > -1
        var isChrome = userAgent.indexOf("Chrome") > -1
            && userAgent.indexOf("Safari") > -1; //判断Chrome浏览器
        if(isIE){
            var word = new ActiveXObject("Excel.Application");
            word.Visible = true;
            word.Documents.Open(Feng.ctxPath + data.file_path);
        }else{
            window.open(Feng.ctxPath + data.file_path);
        }
    }
    //删除上传的资料
    Deal.delete = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/deal/deleteFile", function () {
                Feng.success("删除成功!");
                Deal.search();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("fileId", data.file_id);
            ajax.start();
        };
        Feng.confirm("是否删除该资料" + data.file_name + "?", operation);
    }

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Deal.tableId,
        url: Feng.ctxPath + '/deal/list',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: Deal.initColumn(),
        done: function(res, curr, count){
            $(".layui-table").find("tr").each(function (index) {
                var status = $(this).find('td[data-field="status"]').text().trim();
                if(status=="1"){
                    $(this).css("background-color","#f7fb0b52");
                }
                if(status=="2"){
                    $(this).css("background-color","#FF4500");
                }
            })
        }
    });


    //初始化左侧树
    var year = $("#year").val();
    var ztree = new $ZTree("jobTree", "/deal/tree?year="+year);
    ztree.bindOnClick(Deal.onClickJob);
    ztree.init();

    var treeObj = $.fn.zTree.getZTreeObj("jobTree");
    var nodes = treeObj.getNodes();

    if(nodes[0]!=null && nodes[0].children!=null && nodes[0].children.length>0){
        for(var i=0;i<nodes[0].children.length;i++){
            var node = nodes[0].children[i]
            if(node.color){
                $("#"+node.tId+"_span").css("color",'red');
            }
            if(node!=null && node.children!=null && node.children.length>0){
                for(var j=0;j<node.children.length;j++) {
                    var nod = node.children[j]
                    if(nod.color){
                        $("#"+nod.tId+"_span").css("color",'red');
                    }
                }
            }
        }
    }




    // 工具条点击事件
    table.on('tool(' + Deal.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'upload') {
            Deal.uploadFile(data);
        }else if(layEvent === 'viewFile') {
            //查看文件
            Deal.openFile(data);
        }else if(layEvent === 'delete'){
            Deal.delete(data)
        }
    });

    form.on('select(year)',function (data) {
        var year = data.value;
        //重新初始化树
        var ztree = new $ZTree("jobTree", "/deal/tree?year="+year);
        ztree.bindOnClick(Deal.onClickJob);
        ztree.init();

        var treeObj = $.fn.zTree.getZTreeObj("jobTree");
        var nodes = treeObj.getNodes();

        if(nodes[0]!=null && nodes[0].children!=null && nodes[0].children.length>0){
            for(var i=0;i<nodes[0].children.length;i++){
                var node = nodes[0].children[i]
                if(node.color){
                    $("#"+node.tId+"_span").css("color",'red');
                }
                if(node!=null && node.children!=null && node.children.length>0){
                    for(var j=0;j<node.children.length;j++) {
                        var nod = node.children[j]
                        if(nod.color){
                            $("#"+nod.tId+"_span").css("color",'red');
                        }
                    }
                }
            }
        }
        //重新查询
        Deal.condition.parentId = 0;
        Deal.search()
    })

    /**
     * 导入任务
     */
    $("#btnImport").click(function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '导入任务',
            content: Feng.ctxPath + '/deal/toImport',
            end: function () {
                admin.getTempData('formOk') && table.reload(Deal.tableId);
            }
        });
    })

    /**
     * 任务上报导出
     */
    $("#btnExport").click(function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '任务上报导出',
            content: Feng.ctxPath + '/deal/toExport',
            end: function () {
                admin.getTempData('formOk') && table.reload(Deal.tableId);
            }
        });
    })



})